#!/bin/bash


echo "Installing the env"
echo -e "\n"

# TODO:
#   - test for $VIRTUAL_ENV exists
#   - $VIRTUAL_ENV
#   - code refactoring
root_env_dir="$(dirname `pwd`)"
code_dir="$root_env_dir/ema-webservices"
scripts_dirname="scripts"
scripts_dir="$code_dir/$scripts_dirname"
# download_dir="$root_env_dir/download"
# --suffix not reconized option on (ubuntu 10.04) mktemp (GNU coreutils) 7.4
# download_dir=$(mktemp -d --suffix=.ema)
download_dir=$(mktemp -d)
# TODO: Python version to be detected dynamically
python_version="2.7"
python_home="$root_env_dir/lib/python$python_version"
python_bin_path="$root_env_dir/bin/python"
bname=$(basename `pwd`)

on_error()
{
    post_install_cleaning
    exit 1
}

exec_cmd()
{
	echo "Running: $1"
	# TODO: is that the right way to do it?
	$1
	if [ $? != 0 ]
	then
		echo -e "\n\n!! Error while running: $1 !!\n"
		echo -e "Try rerunning the script or open-up a ticket posting the full output.\n\n"
        on_error
	fi
}

show_help()
{
	echo "Help: $1"
    echo "cd to the $code_dir first"
    echo "then: ./$scripts_dirname/install-env.sh"
	if [ -z "$1" ] # Is parameter #1 zero length?
	then
		exit 0
	else
		exit 1
	fi
}

init_install()
{
	echo "Init install"
	echo "root_env_dir: $root_env_dir"
	echo "code_dir: $code_dir"
	echo "download_dir: $download_dir"
	echo "python_version: $python_version"
	echo "python_home: $python_home"
	echo "python_bin_path: $python_bin_path"
	# echo "python_home: $python_home"

	# if [[  !($bname == $code_dir) ]]
	if [[  !($bname == $(basename $code_dir)) ]]
	then
		show_help "Wrong project dir ($bname != $code_dir)"
	fi

        if [ ! -d $python_home ]
	then
	    python_home="$root_env_dir/lib/python$python_version"
	    echo -e "Can't find $python_home!\n"
	    echo -e "Wrong Python version ($python_version)?\n\n"
	    on_error
	fi

    # the download_dir is now created by mktemp
    # if [ ! -d "$download_dir" ]
    # then
    #     exec_cmd "mkdir $download_dir"
    # fi
	echo -e "\n"
}

post_install_cleaning()
{
	echo "Cleaning temp download directory: $download_dir"
    exec_cmd "rm -rf $download_dir"
}

post_install()
{
	echo "Environment successfully installed"

	echo "You can now run tests"
	echo "$ python manage.py test"
	echo -e "\n"

	echo "If everything is correct, synchronise your database"
	echo "$ python manage.py syncdb"
	echo -e "\n"

	echo "You should now setup database fixtures"
	echo "Read docs/fixtures.txt to see how"
	echo -e "\n"
}

install_pip_requirements()
{
	echo "Installing pip supported requirements"
	exec_cmd "pip install -r docs/requirements.txt"
	echo -e "\n"
}

install_django_simpleopenid()
{
	echo "Installing Django SimpleOpenID"
    if [ -d $python_home/site-packages/simpleopenid ]
    then
        echo "Directory exists: $python_home/site-packages/simpleopenid"
        echo "Requirement already satisfied, skipping"
    else
        exec_cmd "cd $download_dir"
        exec_cmd "wget https://github.com/downloads/lig/simpleopenid/simpleopenid-1.1.2.tar.bz2"
        exec_cmd "tar -xvjf simpleopenid-1.1.2.tar.bz2"
        exec_cmd "cp -r simpleopenid-1.1.2 $python_home/site-packages/simpleopenid"
    fi
	echo -e "\n"
}

patch_django_extensions()
{
    echo "Patching Django Extensions app"
    exec_cmd "cd $python_home/site-packages"
    # https://github.com/django-extensions/django-extensions/issues/issue/17
    echo "Patching against #17"
    exec_cmd "patch -p0 -i $scripts_dir/django_extensions-tests.patch"
    echo -e "\n"
}

install_django_extensions()
{
	echo "Installing Django Extension app"
    if [ -d $python_home/site-packages/django_extensions ]
    then
        echo "Directory exists: $python_home/site-packages/django_extensions"
        echo "Requirement already satisfied, skipping"
    else
        exec_cmd "pip install django-extensions==0.5"
        # patch_django_extensions
    fi

	echo -e "\n"
}

copy_client_secrets_file()
{
    client_secrets_file="client_secrets.json"
	echo "Copying $client_secrets_file file"
    if [ -f $code_dir/$client_secrets_file ]
    then
        echo "File exists: $client_secrets_file"
        echo "Requirement already satisfied, skipping"
    else
        exec_cmd "cp $scripts_dir/$client_secrets_file $code_dir"
    fi
}

# Install modules that are not supported by the requirements.txt file
install_missing_modules()
{
    install_django_simpleopenid
    # install_django_extensions
    copy_client_secrets_file
}

init_install
install_pip_requirements
install_missing_modules
post_install_cleaning
post_install


